import numpy as np
import time
from IPython import display
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import random

Bao = [[0,0],[0,1],[0,2],[0,3],[0,4],[0,5],[0,6],[0,7],[0,8],[0,9],[0,10],[0,11],[0,12],[0,13],[0,14],
       [1,0],[1,1],[1,2],[1,3],[1,4],[1,5],[1,6],[1,7],[1,8],[1,9],[1,10],[1,11],[1,12],[1,13],[1,14],
       [2,0],[2,1],[2,2],[2,3],[2,4],[2,5],[2,6],[2,7],[2,8],[2,9],[2,10],[3,11],[3,12],[3,13],[3,14],
       [3,0],[3,1],[3,2],[3,3],[3,4],[3,5],[3,6],[3,7],[3,8],[3,9],[3,10],[3,11],[3,12],[3,13],[3,14],
       [4,0],[4,1],[4,2],[4,3],[4,4],[4,5],[4,6],[4,7],[4,8],[4,9],[4,10],[4,11],[4,12],[4,13],[4,14],
       [5,0],[5,1],[5,2],[5,3],[5,4],[5,5],[5,6],[5,7],[5,8],[5,9],[5,10],[5,11],[5,12],[5,13],[5,14],
       [6,0],[6,1],[6,2],[6,3],[6,4],[6,5],[6,6],[6,7],[6,8],[6,9],[6,10],[6,11],[6,12],[6,13],[6,14],
       [7,0],[7,1],[7,2],[7,3],[7,4],[7,5],[7,6],[7,7],[7,8],[7,9],[7,10],[7,11],[7,12],[7,13],[7,14],
       [8,0],[8,1],[8,2],[8,3],[8,4],[8,5],[8,6],[8,7],[8,8],[8,9],[8,10],[8,11],[8,12],[8,13],[8,14],
       [9,0],[9,1],[9,2],[9,3],[9,4],[9,5],[9,6],[9,7],[9,8],[9,9],[9,10],[9,11],[9,12],[9,13],[9,14],
       [10,0],[10,1],[10,2],[10,3],[10,4],[10,5],[10,6],[10,7],[10,8],[10,9],[10,10],[10,11],[10,12],[10,13],[10,14],
       [11,0],[11,1],[11,2],[11,3],[11,4],[11,5],[11,6],[11,7],[11,8],[11,9],[11,10],[11,11],[11,12],[11,13],[11,14],
       [12,0],[12,1],[12,2],[12,3],[12,4],[12,5],[12,6],[12,7],[12,8],[12,9],[12,10],[12,11],[12,12],[12,13],[12,14],
       [13,0],[13,1],[13,2],[13,3],[13,4],[13,5],[13,6],[13,7],[13,8],[13,9],[13,10],[13,11],[13,12],[13,13],[13,14],
       [14,0],[14,1],[14,2],[14,3],[14,4],[14,5],[14,6],[14,7],[14,8],[14,9],[14,10],[14,11],[14,12],[14,13],[14,14]]

# 生成含有225个值初始列表，用于存放团簇值的大小
N = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]

N = np.array(N)

M = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]

M = np.array(M)


k = -1  # k是上面列表N的索引控制值
l = 0

def up(i, j):  # 负责控制团簇向上方向遍历
    global l
    global k
    if i == -16:
        i = 14
    if new_arr[i][j] == 0:
        N[l][k] = N[l][k] + 1
        new_arr[i][j] = 2
        e = i
        f = j - 1
        left(e, f)
        g = i
        h = j + 1
        right(g, h)
        i = i - 1
        up(i, j)
    else:
        N[l][k] = N[l][k]

def up2(i, j):  # 负责控制团簇向上方向遍历
    if i == -16:
        i = 14
    if new_arr[i][j] == 2:
        new_arr[i][j] = 3
        e = i
        f = j - 1
        left2(e, f)
        g = i
        h = j + 1
        right2(g, h)
        i = i - 1
        up2(i, j)




def down(i, j):  # 负责控制团簇向下方向遍历
    global k
    global l
    if i == 15:
        i = -15
    if new_arr[i][j] == 0:
        N[l][k] = N[l][k] + 1
        new_arr[i][j] = 2
        e = i
        f = j - 1
        left(e, f)
        g = i
        h = j + 1
        right(g, h)
        i = i + 1
        down(i, j)
    else:
        N[l][k] = N[l][k]


def down2(i, j):  # 负责控制团簇向下方向遍历
    if i == 15:
        i = -15
    if new_arr[i][j] == 2:
        new_arr[i][j] = 3
        e = i
        f = j - 1
        left2(e, f)
        g = i
        h = j + 1
        right2(g, h)
        i = i + 1
        down2(i, j)



def left(i, j):  # 负责控制团簇向左方向遍历
    global k
    global l
    if j == -16:
        j = 14
    if new_arr[i][j] == 0:
        N[l][k] = N[l][k] + 1
        new_arr[i][j] = 2
        a = i - 1
        b = j
        up(a, b)
        c = i + 1
        d = j
        down(c, d)
        j = j - 1
        left(i, j)
    else:
        N[l][k] = N[l][k]


def left2(i, j):  # 负责控制团簇向左方向遍历
    if j == -16:
        j = 14
    if new_arr[i][j] == 2:
        new_arr[i][j] = 3
        a = i - 1
        b = j
        up2(a, b)
        c = i + 1
        d = j
        down2(c, d)
        j = j - 1
        left2(i, j)


def right(i, j):  # 负责控制团簇向右方向遍历
    global k
    global l
    if j == 15:
        j = -15
    if new_arr[i][j] == 0:
        N[l][k] = N[l][k] + 1
        new_arr[i][j] = 2
        a = i - 1
        b = j
        up(a,b)
        c = i + 1
        d = j
        down(c,d)
        j = j + 1
        right(i,j)
    else:
        N[l][k] = N[l][k]



def right2(i, j):  # 负责控制团簇向右方向遍历
    if j == 15:
        j = -15
    if new_arr[i][j] == 2:
        new_arr[i][j] = 3
        a = i - 1
        b = j
        up2(a, b)
        c = i + 1
        d = j
        down2(c, d)
        j = j + 1
        right2(i, j)


def change_color(max):

    i = max[0][0]
    j = max[1][0]
    new_arr[i][j] = 3
    a = i - 1
    b = j
    up2(a, b)
    c = i + 1
    d = j
    down2(c, d)
    e = i
    f = j - 1
    left2(e, f)
    g = i
    h = j + 1
    right2(g, h)




def Sagiri(my_board):
    global N
    global k
    global l
    global new_arr
    global max,old_max


    #for q in range(1):
    bao = random.choice(Bao)
    my_board[bao[0]][bao[1]] = 0
    Bao.remove(bao)
    #print(bao,bao[0],bao[1])



    for i in range(0, 15, 1):
        for j in range(0, 15, 1):
            if my_board[i][j] == 3 or my_board[i][j] == 2:
                my_board[i][j] = 0


    new_arr = my_board

    #print('my_board:',my_board)

    for i in range(0, 15, 1):
        for j in range(0, 15, 1):
            l = i
            k = j
            if new_arr[i][j] == 0:
                N[l][k] = N[l][k] + 1
                new_arr[i][j] = 2
                a = i - 1
                b = j
                up(a, b)
                c = i + 1
                d = j
                down(c, d)
                e = i
                f = j - 1
                left(e, f)
                g = i
                h = j + 1
                right(g, h)
                print('new_Arr:',new_arr)
                print('N[l][k]',N)
            if new_arr[i][j] == 1:  # 当矩阵中值为1时，不计入团簇，并结束该次循坏
                continue

# Some helper functions

# Initialize the board with starting positions




o = 0
p = -1

def update_board(my_board):
    global N
    global k
    global l
    global new_arr
    global max,old_max
    global o,p

    Sagiri(my_board)

    max = np.where(N == np.max(N))
    change_color(max)
    print('max=',max)
    print('N=', N)
    old_max = max
    N = M
    print('++++++++++++')
    new_board = new_arr
    #print(new_board)
    #print(new_board[3])
    return new_board



# Input variables for the board
boardsize = 15 # board will be X by X where X = boardsize
initial_cells = 1 # this number of initial cells will be placed in randomly generated positions

my_board = np.ones((boardsize , boardsize ))
pos_list = []







#????????????????????
print(update_board(my_board))





##### Animate the board #####
# This will throw an error the first time you run the code, but the program will run properly if you
# execute the cell again (there is an error with the animation package that I cannot seem to get rid of)

def animate(frame):
    im.set_array(update_board(my_board))
    return im,


# matplotlib notebook
fig = plt.gcf() #创建画布
im = plt.imshow(my_board, animated=True)


anim = animation.FuncAnimation(fig, animate, frames=200, interval=100, blit=False)
plt.show()



#print(pos_list)
#print(my_board)
#print(pos_list[0][0],pos_list[0][1],pos_list[1][1])


